package com.itheima.book.controller;

import com.itheima.book.interceptor.BaseContext;
import com.itheima.book.utils.result.Result;
import com.itheima.book.pojo.entity.User;
import com.itheima.book.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/profile")
@Api(tags = "个人中心相关接口")
@Slf4j
public class ProfileController {

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    @ApiOperation("获取当前用户信息")
    public Result<User> getUserInfo() {
        log.info("获取当前用户信息");
        // 这里使用固定的用户ID 1，实际应该从登录信息中获取
        User user = userService.getById(BaseContext.getCurrentId());
        log.info("获取当前用户信息:{}",user);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }

    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public Result<Boolean> updateUser(@RequestBody User user) {
        log.info("更新用户信息：{}", user);
        // 设置用户ID为1（实际应该从登录信息中获取）
        user.setId(BaseContext.getCurrentId());
        boolean success = userService.updateById(user);
        return success ? Result.success(true) : Result.error("更新失败");
    }
}